package com.zlfund.cat.zjgsmk.ui.home.smk_user;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zlfund.cat.zjgsmk.R;
import com.zlfund.cat.zjgsmk.ui.base.ZApplication;
import com.zlfund.cat.zjgsmk.ui.setting.SettingActivity;
import com.zlfund.cat.zjgsmk.ui.user.UserManager;
import com.zlfund.cat.zjgsmk.util.DensityUtils;
import com.zlfund.cat.zjgsmk.widget.HeadView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainUserFragment extends Fragment {


    @BindView(R.id.headView)
    HeadView mHeadView;
    Unbinder unbinder;
    @BindView(R.id.iv_icon)
    ImageView mIvIcon;
    @BindView(R.id.ll_user_container)
    LinearLayout mLlUserContainer;

    private final static String[] USER_ITEM_TEXTS = {"我的订单","发起的活动","参加的活动","我的消息","我的动态","我的金币","我关注的话题","我创建的话题","分享好友","联系客服","系统设置"};
    private final static int[] USER_ITEM_ICONS = {R.mipmap.mine_orders,R.mipmap.mine_act_launch,R.mipmap.mine_act_join,R.mipmap.mine_msg,R.mipmap.mine_circles,R.mipmap.mine_gold,R.mipmap.mine_topic_note,R.mipmap.mine_topic_create,R.mipmap.mine_share,R.mipmap.mine_phone,R.mipmap.mine_settings};
    private OnUserItemClickListener mUserItemClickListener;
    private LinearLayout mLlyt;

    public MainUserFragment() {
        // Required empty public constructor
    }

    private class OnUserItemClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case 10:
                    startActivity(new Intent(getContext(), SettingActivity.class));
                    break;
            }
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main_user, container, false);
        unbinder = ButterKnife.bind(this, view);
        mLlyt = (LinearLayout) view.findViewById(R.id.ll_user_container);
        LinearLayout.LayoutParams mLpMarginLarge = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams mLpMarginSmall = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams mLpMarginMax = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mLpMarginLarge.setMargins(0,0,0, DensityUtils.dp2px(getContext(),8));
        mLpMarginSmall.setMargins(0,0,0,DensityUtils.dp2px(getContext(),1));
        mLpMarginMax.setMargins(0,0,0,DensityUtils.dp2px(getContext(),40));
        mUserItemClickListener = new OnUserItemClickListener();
        for (int i = 0; i < USER_ITEM_TEXTS.length; i++) {
            String content = USER_ITEM_TEXTS[i];
            View itemView = createItemView();
            TextView tvContent = (TextView) itemView.findViewById(R.id.tv_content);
            ImageView ivIcon = (ImageView) itemView.findViewById(R.id.iv_icon);
            tvContent.setText(content);
            ivIcon.setImageResource(USER_ITEM_ICONS[i]);
            if (i == 0 || i == 2 || i == 5 || i == 7 || i == 9){
                itemView.setLayoutParams(mLpMarginLarge);
            }else if (i == 10){
                itemView.setLayoutParams(mLpMarginMax);
            }else {
                itemView.setLayoutParams(mLpMarginSmall);
            }
            itemView.setId(i);
            itemView.setOnClickListener(mUserItemClickListener);
            mLlUserContainer.addView(itemView);
        }
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        try {
            ZApplication.mContext.getLoginService().showUserUi(mLlUserContainer, UserManager.singleTon());
        }catch (Exception ignored){}
    }

    @Override
    public void onStart() {
        super.onStart();
        ZApplication.mContext.getLoginService().showUserUi(mLlUserContainer, UserManager.singleTon());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private View createItemView(){
        return LayoutInflater.from(getContext()).inflate(R.layout.item_main_user,null,false);
    }
}
